//
//  WTBarrageView.m
//  MessageCell
//
//  Created by frank on 16/8/8.
//  Copyright © 2016年 yr. All rights reserved.
//

#import "WTBarrageView.h"
#import "WTBarrageObj.h"
#import "BarrageWalkSelfTextSprite.h"
#import "MLEmojiLabel.h"
@interface WTBarrageView()
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) NSDate *startTime;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, assign) NSTimeInterval predictedTime;



@end
@implementation WTBarrageView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initBarrageRenderer];
    }
    
    return self;
}
- (void)startLoadWalkText:(NSArray *)dataArray{

    self.tmp = [NSArray arrayWithArray:dataArray];
    if (_tmp) {
        
        WTBarrageObj * safeObj = [[WTBarrageObj alloc]initWithObject:self withSelector:@selector(autoSendBarrage)];
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.5f target:safeObj selector:@selector(excute) userInfo:nil repeats:YES];
    }
    
    
}
- (void)start{
    WTBarrageObj * safeObj = [[WTBarrageObj alloc]initWithObject:self withSelector:@selector(autoSendBarrage)];
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.5f target:safeObj selector:@selector(excute) userInfo:nil repeats:YES];
}
- (void)insertTextOfData:(NSDictionary *)dict{
    NSMutableArray *des = [[NSMutableArray alloc] init];
    [des addObject:[self walkTextInsertWay:dict]];
    [self.renderer load:des];
    
}
- (void)receiveDataSource:(NSArray *)dataSource{

    NSMutableArray *tmpMarr = [[NSMutableArray alloc] init];
    if (dataSource.count > 2) {
        for (int i = 0; i < 2 ; i ++ ) {
            [tmpMarr addObject:dataSource[i]];
        }
        for (NSDictionary *dic in tmpMarr) {
            [_renderer receive:[self walkTextSpriteDescriptorWithData:dic]];
        }
    }else{
        for (NSDictionary *dic in dataSource) {
            [_renderer receive:[self walkTextSpriteDescriptorWithData:dic]];
        }
    }
    [_renderer stop];
    [_timer invalidate];
   
}
- (void)autoSendBarrage{

    NSInteger spriteNumber = [_renderer spritesNumberWithName:nil];
    if (spriteNumber <= 3) {
        
        [self receiveDataSource:self.tmp];
        
    }
    [self.renderer start];
    
}

- (void)initBarrageRenderer{
    _index = 0;
    _predictedTime = 0.0f;
    self.renderer.canvasMargin = UIEdgeInsetsMake(10, 10, 5, 30);
    self.renderer = [[BarrageRenderer alloc] init];
    self.renderer.delegate = self;
    self.renderer.redisplay = YES;
    [self addSubview:self.renderer.view];
    [self sendSubviewToBack:self.renderer.view];
    
}
- (BarrageDescriptor *)walkTextSpriteDescriptorWithData:(NSDictionary *)dict
{
    BarrageDescriptor * descriptor = [[BarrageDescriptor alloc]init];
    descriptor.spriteName = NSStringFromClass([BarrageWalkSelfTextSprite class]);
    
    descriptor.params[@"text"] = [NSString stringWithFormat:@"%@:%@",dict[@"name"],dict[@"text"]];
    descriptor.params[@"textColor"] = [UIColor whiteColor];
    descriptor.params[@"trackNumber"] = @4;
    descriptor.params[@"speed"] = @(100 * (double)random()/RAND_MAX+50);
    descriptor.params[@"direction"] = @(1);
    descriptor.params[@"cornerRadius"] = @15;
    descriptor.params[@"backgroundColor"] = [UIColor colorWithWhite:0 alpha:0.5];

    return descriptor;
}

- (BarrageDescriptor *)walkTextInsertWay:(NSDictionary *)dict
{
    BarrageDescriptor * descriptor = [[BarrageDescriptor alloc]init];
    descriptor.spriteName = NSStringFromClass([BarrageWalkSelfTextSprite class]);
    
    
    descriptor.params[@"text"] = [NSString stringWithFormat:@"%@:%@",dict[@"name"],dict[@"text"]];
    descriptor.params[@"textColor"] = [UIColor redColor];
    descriptor.params[@"speed"] = @(100 * (double)random()/RAND_MAX+50);
    descriptor.params[@"direction"] = @(1);
    descriptor.params[@"cornerRadius"] = @15;
    descriptor.params[@"backgroundColor"] = [UIColor colorWithWhite:0 alpha:0.5];
    
    return descriptor;
}

- (void)dealloc{
    [self.renderer stop];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
